package com.wuyan.web.upload.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wuyan.web.base.helper.PubConfigHelper;
import com.wuyan.web.base.helper.rep.RepPageData;
import com.wuyan.web.base.helper.req.CustomQueryHelper;
import com.wuyan.web.upload.entity.PubConfig;
import com.wuyan.web.upload.service.PubConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 启动配置
 *
 * @author wuyan
 */

@Slf4j
@Component
public class AfterRunnerConfig implements CommandLineRunner {

    @Autowired
    private PubConfigHelper configHelper;

    @Autowired
    private PubConfigService pubConfigService;

    @Override
    public void run(String... args) throws Exception {
        initConfig();
    }

    /**
     * 初始化配置
     */
    private void initConfig()
            throws JsonProcessingException, InvocationTargetException,
            IllegalAccessException, InstantiationException, NoSuchMethodException, ClassNotFoundException {

        CustomQueryHelper<PubConfig> queryHelper = CustomQueryHelper.init(PubConfig.class, "[]", "[]");

        RepPageData<PubConfig> data = pubConfigService.page(queryHelper.getPredicates(), queryHelper.getOrders(), false, 0, 0);

        List<PubConfig> list = data.getList();

        if (null == list || list.isEmpty()) {
            return;
        }

        configHelper.setConfigs(PubConfigHelper.convert(list));
    }
}
